Get to Know Our Way of R

👤Ondřej Mottl

Science School of Quantitative Ecology 2025

bit.ly/SSoQE

This presentation


Learning Objectives

What we’ll cover today

🎯 Main Topics

  • Navigate RStudio interface
  • R code execution & understanding environments
  • R Projects
  • Verify your setup

By the end, you’ll be able to:

  • Find your way around RStudio
  • Understand R environments and memory usage
  • Use R Projects to have reproducible pathing
  • Be ready for SSoQE 2025!

R

R is …

  • A programming language for statistical computing and graphics
  • Widely used in academia and industry
  • Open-source and free to use
  • Highly extensible with packages
  • Supported by a large and active community

GUI

GUI = Graphical User Interface

The choice is yours!

RStudio IDE

📝 Source Panel



  • Write and edit your R scripts
  • Save your code for later
  • Syntax highlighting & autocomplete

💻 Console Panel



  • Interactive R session
  • See output and results
  • Quick calculations

🗂️ Environment Panel



  • Variables in memory
  • Data objects
  • History of commands
  • Version control (git)

📊 Files/Plots Panel



  • File browser
  • Plot output
  • Package management
  • Help documentation

Note on practical exercises

Practical Exercise


  1. Use keyboard shortcuts to run code in the console
  2. Customize RStudio appearance (theme, font size)

05:00

Execution & Environments

Running Code

🖥️ Console

x <- 5
y <- 10
result <- x + y
print(result)
[1] 15
  • all objects created in the console are stored in the memory (~ RAM)
  • objects are temporary!!! and will be lost when you close R/RStudio (restart session)

🗑️ Memory Management

# Remove specific objects
rm(data_experiment)

👀 Environment Panel

  • Objects: See contents of memory
  • Types: Data frames, vectors, functions
  • Memory usage: RAM consumption

Workspace vs Session

💾 Workspace

  • .RData can save objects from previous session
  • Never do that!!!
  • Settings: Tools → Global Options → General
    • Uncheck “Restore .RData into workspace”
    • “Save workspace to .RData on exit”: “Never”
  • clean R environment often
    • Shortcut: Ctrl + Shift + F10

📝 What to save?

  • Everything is temporary unless saved on disk
  • save scripts, not objects
  • save important data on drive (as CSV or RDS)

Best Practice

  • Start fresh each session
  • Write all code in scripts
  • Recreate objects from code / load data
  • Ensure reproducibility

Practical Exercise


  1. Setup RStudio to never save or load .RData
  2. Clear your environment using the button or shortcut

R Projects

Project file structure

Project structure - RStudio Projects

R-studio is using Projects already by default


What is an R Project?

📁 Self-Contained Workspace

  • .Rproj file in root directory
  • Isolated working directory
  • Usage by other software (e.g. Git)

🎯 Benefits

  • Isolation: No interference between projects
  • Organization: Clear folder structure
  • Reproducibility: Relative file paths
  • Version Control: Git integration

Practical Exercise


  1. Create a new (RStudio) project
  2. Switch between projects

05:00

File Paths & the {here} Package

Working Directory



@JennyBryan:

If the first line of your R script is

setwd("C:/Users/jenny/path/that/only/I/have")

I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.

The {here} package is up for rescue!

The Path Problem


What Goes Wrong

# This breaks on other computers:
setwd("C:/Users/John/Documents/Project")
data <- read.csv("../data/file.csv")

# Different on Mac/Linux:
setwd("/Users/john/Documents/Project")

# Fragile and non-reproducible
source("C:/path/to/my/script.R")

😱 Common Issues

  • Code breaks on different computers
  • Collaborators can’t run your code
  • Different operating systems
  • Moving project folders

Exercise

  1. install the {here} package
  2. try running here::here() in your R console
  3. What is the difference between relative and absolute paths?

03:00

Exercise - rewrite the code using {here}

# original code
data <- 
  read.csv(
    "C:/Users/jenny/path/that/only/I/have/Data/file.csv"
  )

source(
  "C:/Users/jenny/path/that/only/I/have/R/functions.R"
)
# solution
library(here)

data <- 
  read.csv(
    here::here(
      "Data/file.csv"
    )
  )

source(
  here::here(
    "R/functions.R"
  )
)

Setup & Check

Required Software Checklist


Install/update R

Make sure you have the latest version of R installed. You can check the version by running:

R.version.string
[1] "R version 4.5.1 (2025-06-13 ucrt)"

Install the newest version of R from CRAN.

Bonus

Check out {updateR} package to update R and all installed packages.

Install/update RStudio

Make sure you have the latest version of RStudio installed. You can check the version by running:

rstudioapi::versionInfo()$version
# ‘2025.5.1.513’

You can download the newest version of RStudio from RStudio website.

Rtools (Windows only)

Rtools is a collection of tools necessary for building R packages on Windows.

You can download Rtools from CRAN Rtools page.

You need version compatible with your R version. For R 4.5+, use Rtools 4.5 or later.

{usethis} package

The {usethis} package is a collection of functions that help automate package and project setup tasks.

Install the package by running:

install.packages("usethis")

R packages

Install all required packages


Go to SSoQE - pre-workshop setup and run the provided code chunk in your R console to install all required packages.

Version Control

Git

  • local software
  • keep track of changes of files


GitHub

  • host server
  • store the (git) data
  • project management, collaboration, publishing

Half the battle “git hell”

Getting all the necessary software installed, configured, and playing nicely together is honestly half the battle … Brace yourself for some pain

GitHub

Making the account

You will need a GitHub account.

It is super easy to create one, just go to GitHub.com and follow the instructions.


Some tips

  • Use a professional username, preferably your name
  • Use university/science institute email address

Git

🐚 shell intermezzo

  • You will need to use the shell (command line) to interact with Git.
  • The actual tool might differ based on your operating system.
  • Please refer to this document for more information.

If you are using RStudio, you can use the built-in terminal (NOT Console).

Installation

Is Git already installed?

Check your version:

git --version
## git version 2.42.0.windows.2

Installation

Download and install Git for Windows

Download and install Git for MacOS

Ubuntu or Debian Linux:

sudo apt-get install git

Fedora or RedHat Linux:

sudo yum install git

Configuration

Introduce yourself to Git 👋

❗Note to substitute your name and the email address❗

Use the same email address as the one you used for GitHub account.

git config --global user.name "Jane Doe"
git config --global user.email "jane@example.com"
git config --global --list

Can you hear me now?

We now need to make sure that RStudio can communicate with Git.

Step 1 - Git

Confirm that git has your name and email by running:

usethis::use_git()

If needed, set the name and email by running:

# replace with your name and email
usethis::use_git_config(user.name = "Jane Doe", user.email = "jane@example.com")

Can you hear me now?

We now need to make sure that RStudio can communicate with Git.

Step 2 - PAT (Personal Access Token)

You can check more details about it here and here.

usethis::create_github_token()

Click Generate token and copy the token to a safe place.

Can you hear me now?

Add the PAT to RStudio by running:

Step 3 - RStudio3

gitcreds::gitcreds_set()

Can you hear me now?

Other useful functions:

# Read about PAT
usethis::gh_token_help()

# List all git settings
usethis::git_sitrep()

# Check if PAT is set
gh::gh_whoami()

If everything fails, you can try to set up the PAT manually into your .Renviron file.

Simply run:

usethis::edit_r_environ()

Add a line like this, but substitute your PAT:

GITHUB_PAT=ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Outro

SPROuT

The materials used in this presentation are further expanded in a greater detail in course Science Powered through Reproducibility, Openness, and Teamwork (SPRouT) taught at Charles University.

About me

Ondřej Mottl Assistant Professor at Charles University

Head of the 🧑‍💻 Laboratory of Quantitative Ecology